<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>设置我的资料</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header">设置我的资料</div>
        <div class="layui-card-body" pad15>
          <form class="layui-form">
            <input hidden name="id" th:value="${session.admin.id}">
            <div class="layui-form-item">
              <label class="layui-form-label">我的角色</label>
              <input type="hidden" id="role_name" th:value="${session.role.desc}">
              <input type="hidden" id="role_id" name="role_id" th:value="${session.role.id}">
              <div class="layui-input-inline">
                <select name="role" lay-verify="">
                  <option selected>管理员
                  </option>
                </select>
              </div>
              <div class="layui-form-mid layui-word-aux">当前角色不可更改为其它角色</div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">用户名</label>
              <div class="layui-input-inline">
                <input type="text" name="name" th:value="${session.admin.name}" readonly class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">不可修改。一般用于后台登入名</div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">性别</label>
              <div class="layui-input-block" th:if="${session.admin.sex} eq '男'">
                <input type="radio" name="sex" value="男" title="男" checked>
                <input type="radio" name="sex" value="女" title="女">
              </div>
              <div class="layui-input-block" th:if="${session.admin.sex} eq '女'">
                <input type="radio" name="sex" value="男" title="男">
                <input type="radio" name="sex" value="女" title="女" checked>
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">头像</label>
              <div class="layui-input-inline">
                <input name="img" lay-verify="required" id="pic"
                       placeholder="图片地址" th:value="${session.admin.img}" class="layui-input">
              </div>
              <div class="layui-input-inline layui-btn-container" style="width: auto;">
                <button type="button" class="layui-btn layui-btn-primary" id="adminPicUpload">
                  <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
                <button class="layui-btn layui-btn-primary" id="showPic" >查看图片</button >
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">手机</label>
              <div class="layui-input-inline">
                <input type="text" name="phone" th:value="${session.admin.phone}" lay-verify="phone" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">邮箱</label>
              <div class="layui-input-inline">
                <input type="text" name="email" th:value="${session.admin.email}" lay-verify="email" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="setmyinfo">确认修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重新填写</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'set', 'layer', 'upload','form'], function () {
        var $ = layui.$,
            upload = layui.upload,
            form = layui.form,
            layer = layui.layer;
        var uploadInst = upload.render({
            elem: '#adminPicUpload' //绑定元素
            , url: '/upload' //上传接口
            , accept: 'file'
            , size: 0
            , done: function (res) {
                //上传完毕回调
                if (res.code == 200) {
                    $("#pic").val(res.msg);
                    console.log(res);
                    layer.msg('文件上传成功',
                        {
                            icon: 1,
                            time: 800,
                        });
                } else {
                    layer.msg('文件上传失败');
                }
            }
            , error: function () {
                //请求异常回调
            }
        });
        $('#showPic').on('click', function() {
            layer.photos(
                {photos: {title: "查看头像", data: [{src: $("#pic").val()}]}, shade: .01, closeBtn: 1, anim: 5}
            )
            return false;
        });
        form.on('submit(setmyinfo)', function(data){
            console.log(JSON.stringify(data.field));
            $.ajax({
                type:"post",
                contentType : 'application/json',
                url:"/admin/updateAdmin",
                data:JSON.stringify(data.field),
                dataType : 'json',
                success:function(data) {
                    if(data.code=='200'){
                        layer.msg('修改成功',
                            {
                                icon:1,
                                time: 1000
                            },function(){
                                window.location.reload();
                            })
                    }
                    else {
                        layer.alert('修改失败！', {icon: 2});
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>